import Vue from 'vue';
import Vuex from 'vuex';
import * as api from '@/api/api';
Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    userInfo: null
  },
  mutations: {
    /** 获取个人信息 */
    async userFun (state, routeUrl) {
      const data = await api.info();
      // data.identity = 'FINANCIAL';
      state.userInfo = data;
      wx.setStorageSync('userInfo', data);
      if (routeUrl) {
        if (data.reg) {
          var arr = ['pages/store/store/main',
            'pages/index/index/main',
            'pages/PersonalCenter/personal/main',
            'pages/moneyMan/moneyMan/main',
            'pages/money/money/main'
          ];

          var res = arr.find(function (elem) {
            if (elem === routeUrl) {
              return true;
            } else {
              return false;
            }
          });
          if (res) {
            wx.reLaunch({
              url: `/${routeUrl}`
            });
          } else {
            wx.navigateTo({
              url: `/${routeUrl}`
            });
          }
        } else {
          wx.redirectTo({
            url: '/pages/index/children/register/main?index=3'
          });
        }
      }
    }

  },
  actions: {},
  getters: {
    userInfo: state => {
      return state.userInfo || wx.getStorageSync('userInfo');
      // : wx.getStorageSync('userInfo')
    }
  }
});
